package com.example.jiaju.ui;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.room.Room;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.jiaju.MainActivity;
import com.example.jiaju.R;
import com.example.jiaju.adapter.ConsultAdapter;
import com.example.jiaju.dao.ConsultDao;
import com.example.jiaju.database.ConsultDaoDatabase;
import com.example.jiaju.entity.Consult;

import java.util.ArrayList;
import java.util.List;

/**
 * 咨询页面
 */
public class ConsultFragment extends Fragment {


    protected List<Consult> consultList=new ArrayList<Consult>();
    protected RecyclerView recyclerView;
    protected ConsultAdapter consultAdapter;
    private ConsultDao dao;
    private ConsultDaoDatabase database;
    public ConsultFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view=inflater.inflate(R.layout.fragment_consult, container, false);

        //1.和数据库建立连接
        database= Room.databaseBuilder(getActivity(),ConsultDaoDatabase.class,"consult_database")
                .allowMainThreadQueries()//允许在主线程调用
                .build();

        //2.返回的不是一个接口对象，返回的是一个接口的实现类  是运行期间动态生成的
        dao=database.getConsultDao();

        //3.操作数据库
        //查询数据
        consultList=dao.findAllConsult();

        recyclerView=view.findViewById(R.id.consult_recyclerView);
        consultAdapter=new ConsultAdapter(consultList,getActivity());
        //填充布局文件
        recyclerView.setAdapter(consultAdapter);
        /*设置布局文件的显示方式*/
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        return view;
    }


}